package com.wzy.jpademo1.service;

import com.wzy.jpademo1.dao.UserRepository;
import com.wzy.jpademo1.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Optional;

@Service
public class UserService {
    @Resource
    private UserRepository userRepository;

    @Transactional
    public User save(User user){
        return userRepository.save(user);
    }

    @Transactional
    public void delete(int id){
        userRepository.deleteById(id);
    }

    @Transactional
    public void update(User user){
        user.setUsername("孙悟空");
        user.setLoginName("swk");
    }

    public Iterable<User> getAll(){
        return userRepository.findAll();
    }

    public User getById(Integer id){
       Optional<User> op = userRepository.findById(id);
       return op.get();
    }
}
